# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: xguo

import json
import os
import time
import tornado.ioloop
import tornado.web

record_list = []

class MainHandler(tornado.web.RequestHandler):
  def get(self):
    sess = self.get_argument('session')
    index = self.get_argument('index')
    interval = self.get_argument('interval')
    data = {
      'session': sess, 
      'index': index,
      'interval': interval,
      'ts': time.time_ns(),
    }
    self.write("ok %s, %s" % (sess, index))
    record_list.append(data)



class SaveHandler(tornado.web.RequestHandler):
  def get(self):
    self.write("save ok")
    with open('server.json', 'w') as outfile:
      json.dump(record_list, outfile)


def make_app():
  return tornado.web.Application(
             [
                (r"/", MainHandler),
                (r"/save", SaveHandler)
             ])


if __name__ == "__main__":
  app = make_app()
  app.listen(8080)
  print('sever started!')
  tornado.ioloop.IOLoop.current().start()
